package com.shanghaionstar.activity.webview.handler;

import android.os.Bundle;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.more.response.GetContentDetailResponseType;
import com.shanghaionstar.activity.iview.ICommonWebviewActivityView;
import com.shanghaionstar.user.service.GuideDetailService;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/11.
 */
public class CommonWebviewPresenter extends BasePresenter {
    private BaseActivity mContext;
    private ICommonWebviewActivityView mDetailView;
    private GuideDetailService mService;

    public CommonWebviewPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        mDetailView = (ICommonWebviewActivityView) view;
        mContext = (BaseActivity) view.onActivity();
        mService = new GuideDetailService(mContext);
    }

    public void getCategoryContentById(Bundle bundle){
        String detailId = bundle.getString("detailId");
        CategoryType categoryType = (CategoryType) bundle.getSerializable("categoryType");
        mDetailView.setLoading(true);
        subscription=mService.getCategoryContentById(detailId,categoryType).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<GetContentDetailResponseType>() {
                    @Override
                    public void call(GetContentDetailResponseType contentListResponse) {
                        mDetailView.setLoading(false);
                        mDetailView.onReponseSuccess(contentListResponse);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mDetailView.setLoading(false);
                        if(throwable != null)
                            mDetailView.onResponseError((OnStarException) throwable);
                    }
                });
    }
}
